# -*- coding: utf-8 -*-
import logging
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException

from objectsmap.page_locators import Downloads
from pages.base_page import BasePage
from pages_exceptions.pages_exceptions import DownloadException


class DownloadsPage(BasePage):
    TITLE = 'Download overview for the IASO hybrid cloud backup software.'
    LOCATION = 'https://www.iaso.com/downloads'

    def __init__(self, driver):
        super(DownloadsPage, self).__init__(driver, DownloadsPage.LOCATION)

    def is_right_location(self):
        super(DownloadsPage, self).is_right_location(DownloadsPage.TITLE)

    def download_product(self, product):
        try:
            product_element = self._driver.find_element(*product)
            product_element.click()
            logging.info("download success")
        except (TimeoutException, NoSuchElementException) as e:
            raise DownloadException("download failed")

    def read_more(self):
        try:
            read_more_element = self._driver.find_element(*Downloads.BTN_READ_MORE)
            read_more_element.click()
            logging.info("read_more success")
        except (TimeoutException, NoSuchElementException) as e:
            raise DownloadException("read_more failed")
